<!--Page to display the list of questions according to the search string -->
 <%@ include file="header.jsp" %>
 <script type="text/javascript">
			function setCurrentMenu(){
			}
</script>

 <%
 	if (session.getAttribute("pageName")==null) {	
		response.sendRedirect("Error.jsp");
 	} else {
		String pageName=(String)session.getAttribute("pageName");
		if (pageName.equals("search")) { 
%>
		<h2>Search Results</h2>
		<hr />
		 <script type="text/javascript">
			function setCurrentMenu(){
				
				document.getElementById("4").setAttribute('style', 'background:#999999');
				document.getElementById("2").setAttribute('style', 'background:#999999');
				document.getElementById("3").setAttribute('style', 'background:#999999');
				document.getElementById("1").setAttribute('style', 'background:#0093e7');
			}
			</script>			
<%		List<Questions> questions = (List<Questions>)session.getAttribute("listOfQuestions"); %>
		<h3> <%=questions.size()%> Questions Found for " <%= (String)session.getAttribute("searchString") %> "</h3>		
<%		} else if (pageName.equals("home")) { %>
				<h2>Most Recent Questions</h2>				
				<hr />
				 <script type="text/javascript">
					function setCurrentMenu(){
						
						document.getElementById("4").setAttribute('style', 'background:#999999');
						document.getElementById("2").setAttribute('style', 'background:#999999');
						document.getElementById("3").setAttribute('style', 'background:#999999');
						document.getElementById("1").setAttribute('style', 'background:#0093e7');
					}
					</script>
<% 		} else if (pageName.equals("Unanswered Questions")) { %>
				<h2>Unanswered Questions</h2>				
				<hr />
				 <script type="text/javascript">
					function setCurrentMenu(){
						
						document.getElementById("1").setAttribute('style', 'background:#999999');
						document.getElementById("2").setAttribute('style', 'background:#999999');
						document.getElementById("4").setAttribute('style', 'background:#999999');
						document.getElementById("3").setAttribute('style', 'background:#0093e7');
					}
					</script>
	<% } 
 	}
%>
 
 <%	 if(session.getAttribute("listOfQuestions")!= null) { 
		List<Questions> questions = (List<Questions>)session.getAttribute("listOfQuestions");
		if(!questions.isEmpty()){
%>	
<% 	    int numPages = 0;
		int count = 0;
		int increment = 1;
		int numRows = 0;

		String startIndexString = request.getParameter("startIndex");

		if(startIndexString == null) {
			startIndexString = "0";
		}

		int startIndex = Integer.parseInt(startIndexString);

		try {
			
			numRows = questions.size();

			int numRecordsPerPage = 5;
			
			numPages = numRows /numRecordsPerPage ;

			int remain = numRows % numRecordsPerPage ;
			if(remain != 0){
				numPages = numPages +1 ;
			}

			if((startIndex + numRecordsPerPage) <= numRows) {
				increment = startIndex + numRecordsPerPage ;
			} else {

				if(remain == 0){
					increment = startIndex + numRecordsPerPage ;
				} else {
					increment = startIndex + remain;
				}
			}

			for(count = startIndex; count < increment; count++) {
%>

<!-- Showing all the questions. -->
	<div style="margin:10px;">
		<p><a style="text-decoration: none" href="ShowQuestion?id=<%= questions.get(count).getId() %>"><h3><%=questions.get(count).getTitle() %></h3></a></p>  
		   <br />
		   <% Questions question= questions.get(count);  
			  int questionId=question.getId();
			  request.setAttribute("question_id",questionId+"");
    	   %>
           <jsp:include page="RetrieveTags.jsp" flush="true">
		   		<jsp:param value="-1" name="question_id"/>
		   </jsp:include>
		   <div align="right" >
				asked <%= questions.get(count).getPosted()%>
				<br />
				<%= new UsersDAO().findById(questions.get(count).getUser_id()).getName()%>
			</div>	
			<br />	
			<hr style="color:gray;" />	
	</div>	
<%	} %>
			<br>
			<br>
<div style="position: absolute;bottom: 5px;right: 50px;">				
			Displaying Records:		
<% if(startIndex + numRecordsPerPage < numRows) { %>
		<%= " " + (startIndex+1) %> - <%= increment  %>			
<% } else { %>			
<%= " " + (startIndex+1) %> - <%= numRows %>
<% }%>
 of <%= questions.size() %> 
<%	if(startIndex != 0) { %>
		&nbsp;
		<a href="RetrieveQuestion.jsp?startIndex=<%=startIndex-numRecordsPerPage%>">Previous</a>&nbsp;
<% } %>

<%	increment += numRecordsPerPage ; %>
<%			if(startIndex + numRecordsPerPage < numRows) { %>
				<a href="RetrieveQuestion.jsp?startIndex=<%=startIndex+numRecordsPerPage %>">Next</a>
<% } %>

<%  } catch (Exception exc) {
			out.println(exc.toString());
		} // end try-catch
%>
	
<%	} 
}%>
</div>  
  
<%@ include file="footer.jsp" %>
  